<?php
//error_reporting(0);
//TODO:discuz的转换
@set_time_limit(0);
include_once('../../inc/init.php');
include_once('../../../config.inc.php');
include_once('../../inc/cache.php');
include_once('../../admin/inc/adminfun.php');

$_OLDDB['hostname']=$dbhost;
$_OLDDB['username']=$dbuser;
$_OLDDB['password']=$dbpw;
$_OLDDB['database']=$dbname;
$_OLDDB['charset']='utf8';
$_OLDDB['prefix']=$tablepre;


$db->pconnect=0;
$olddb = new db();
$olddb->pconnect=0;
$olddb->connect($_OLDDB);


$action=$_GET["action"];
switch($action){
	case "goStep1":
		//TODO:
		$db->row_query("DELETE FROM `{$_DB['prefix']}forums`");
		$rows=$olddb->row_query("SELECT f1.fid as fid,fup,name,f2.description as description,threads,posts,displayorder,f2.keywords as keywords FROM `{$tablepre}forums` f1,`{$tablepre}forumfields` f2 WHERE f1.fid=f2.fid");
		foreach($rows as $row){
			$f=array();
			$f['id']=$row['fid'];
			$f['pid']=$row['fup'];
			$f['forumname']=$row['name'];
			$f['forumcontent']=$row['description'];
			$f['topicsnum']=$row['threads'];
			$f['postsnum']=$row['posts'];
			$f['ordernum']=$row['displayorder'];
			$f['metakeywords']=$row['keywords'];
			$db->row_insert("forums",$f);
		}
		exit("y");
	break;

	case "goStep2":
		$hassalt=$db->row_query("Describe `{$_DB['prefix']}users` salt");
		if(empty($hassalt)){
			@$db->row_query("ALTER TABLE `{$_DB['database']}`.`{$_DB['prefix']}users` ADD COLUMN `salt` VARCHAR(10) NOT NULL AFTER `credits8`");
		}
		//@$db->row_query("ALTER TABLE `{$_DB['database']}`.`{$_DB['prefix']}users` ADD COLUMN `salt` VARCHAR(10) NOT NULL AFTER `credits8`");
		//@$db->row_query("ALTER TABLE `{$_DB['database']}`.`{$_DB['prefix']}users` DROP COLUMN `salt`");

		$db->row_query("DELETE FROM `{$_DB['prefix']}users`");
		$rows=$olddb->row_query("SELECT *,ucm.password as passowrd,ucm.regip as regip FROM `{$tablepre}members` m,`{$tablepre}uc_members` ucm Where m.uid=ucm.uid");
		foreach($rows as $row){
			$u=array();
			$u['id']=$row['uid'];
			$u['username']=addslashes($row['username']);
			$u['userpass']=$row['password'];
			switch(intval($row['adminid'])){
				case 1:
					$u['groupid']=GROUP_ADMIN;
				break;
				case 2:
					$u['groupid']=GROUP_SUPERADMIN;
				break;
				case 3:
					$u['groupid']=GROUP_FORUMADMIN;
				break;
				case 4:
					$u['groupid']=GROUP_NOPOST;
				break;
				case 8:
					$u['groupid']=GROUP_NOVERIFY;
				break;
				default:
					$u['groupid']=1;
				break;
			}

			$u['email']=$row['email'];
			$u['signuptime']=$row['regdate'];
			$u['signupip']=is_ip($row['regip'])?$row['regip']:"";
			$u['credits1']=$row['extcredits1'];
			$u['credits2']=$row['extcredits2'];
			$u['credits3']=$row['extcredits3'];
			$u['credits4']=$row['extcredits4'];
			$u['credits5']=$row['extcredits5'];
			$u['credits6']=$row['extcredits6'];
			$u['credits7']=$row['extcredits7'];
			$u['credits8']=$row['extcredits8'];
			$u['salt']=$row['salt'];
			$db->row_insert('users',$u);
		}
		exit("y");
	break;
	case "goStep3":
		$db->row_query("DELETE FROM `{$_DB['prefix']}topics`");
		$db->row_query("DELETE FROM `{$_DB['prefix']}posts`");
		$rows=$olddb->row_query("SELECT * FROM `{$tablepre}threads`");
		foreach($rows as $row){
			$t['id']=$row['tid'];
			$t['fid']=$row['fid'];
			$t['userid']=$row['authorid'];
			$t['username']=addslashes($row['author']);
			$t['title']=addslashes($row['subject']);
			$t['posttime']=$row['dateline'];
			$t['edittime']=$row['dateline'];
			$t['ordertime']=$row['lastpost'];
			$t['lastreply']=$row['lastpost']."\t".addslashes($row['lastposter']);
			//$t['topictype']=$row[''];
			$t['replynum']=$row['replies'];
			$t['hits']=$row['views'];
			$db->row_insert('topics',$t);
		}
		$rows=$olddb->row_query("SELECT * FROM `{$tablepre}posts`");
		foreach($rows as $row){
			$p['id']=$row['pid'];
			$p['tid']=$row['tid'];
			$p['istopic']=$row['first'];
			$p['fid']=$row['fid'];
			$p['userid']=$row['authorid'];
			$p['username']=addslashes($row['author']);
			$p['title']=addslashes($row['subject']);
			$p['content']=addslashes($row['message']);
			$p['posttime']=$row['dateline'];
			$p['ip']=is_ip($row['useip'])?$row['useip']:"";
			$db->row_insert('posts',$p);
		}
		exit("y");
	break;
	case "goStep4":
		$db->row_query("DELETE FROM `{$_DB['prefix']}attachments`");
		$rows=$olddb->row_query("SELECT * FROM `{$tablepre}attachments`");
		foreach($rows as $row){
			$p['id']=$row['aid'];
			$p['tid']=$row['tid'];
			$p['postid']=$row['pid'];
			$p['userid']=$row['uid'];
			$p['username']="";
			$p['filename']=$row['filename'];
			$p['filesize']=$row['filesize'];
			$p['filepath']=$row['attachment'];
			$p['downloadnum']=$row['downloads'];
			$p['uploadtime']=$row['dateline'];
			$p['type']=$row['isimage'];
			$p['filetype']=$row['filetype'];
			$p['imgwidth']=$row['width'];
			$db->row_insert('attachments',$p);
		}
		$db->row_query("UPDATE `{$_DB['prefix']}attachments` att SET username=(Select username from `{$_DB['prefix']}users` Where id=att.userid)");
		cache();
		exit("y");
	break;

}

function cache(){
		writeAdsCache();
		writeAnnouncesCache();
		writeCreditsCache();
		writeForumsCache();
		writeGroupsCache();
		writeLinksCache();
		writeSettingsCache();
		writeStampsCache();
		writeTemplatesCache();
		writePluginsCache();
}

?>
